
package org.feerepository.client.controller;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.feerepository.client.model.AreaModel;
import org.feerepository.client.views.IAreasView;

public class AreasController implements RequestCallback
{
    private List<IAreasView> views = new ArrayList<IAreasView>();
    private Collection<AreaModel> model = new ArrayList<AreaModel>();

    private static AreasController instance;

    public static AreasController getInstance()
    {
        if(instance == null)
            instance = new AreasController();
        return instance;
    }

    public void addView(IAreasView view)
    {
        views.add(view);
    }

    public void onResponseReceived(Request request, Response response)
    {
        parseResponse(response.getText());

        for(IAreasView view: views)
            view.updateView(model);
    }

    public void onError(Request request, Throwable exception) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
/**
 * Populates the model fromt the xml
 * @param text
 */
    private void parseResponse(String text)
    {
         Document doc = XMLParser.parse(text);
              //  Element root = (Element)doc.getElementsByTagName("areas").item(0);
              //  NodeList areas = root.getElementsByTagName("area");
                NodeList areas = doc.getElementsByTagName("area");
                for(int i=0; i<areas.getLength();i++)
                {
                    Element area = (Element) areas.item(i);
                  // Element areaElement =  (Element) area.getElementsByTagName("areaname").item(0);
                   String id = area.getAttribute("id");
                   String areaName = area.getElementsByTagName("areaname").item(0).getFirstChild().getNodeValue();
                   NodeList jurisdictions = area.getElementsByTagName("jurisdiction");
                   for(IAreasView view : views)
                   {
                       view.addArea(areaName,Integer.parseInt(id));
                       parseJurisdiction(jurisdictions, view);

                   }
                }
                   //TODO: populate the view
                   //app.getMainMenu().addItem(areaName);
                
    }

    private void parseJurisdiction(NodeList jurisdictions, IAreasView view)
    {
       for(int i=0;i<jurisdictions.getLength();i++)
       {
         Element jurisdictionElement = (Element)jurisdictions.item(i);
        String jurisdictionName = ((Element)jurisdictionElement.getElementsByTagName("name").item(0)).getFirstChild().getNodeValue();
            view.addJurisdiction(jurisdictionName,Integer.parseInt(jurisdictionElement.getAttribute("id")));
       }

    }

}
